﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KaartenLib;
using ClassLibrary1;
using System.Collections;
using System.Data.SqlClient;

namespace WindowsFormsApplication2
{
    public partial class UitvoerenOpdracht : Form
    {
        private Int32 opdrachtID;
        private Int32 leerlingID;
        private List<Oefening> opdrachten;
        private List<String> answers;
        private int excersiseNr = -1;
        private int tijd = 50;
        private Timer timer;
        private int cycle;
        private DatabaseActies acties;

        public UitvoerenOpdracht()
        {
            InitializeComponent();

            acties = new DatabaseActies();

            timer = new Timer();
            timer.Tick += new EventHandler(timing);
            timer.Interval = 100;

            KiesLeerlingenForm form = new KiesLeerlingenForm(this);
            form.VisibleChanged += otherFormClosed;
            form.ShowDialog();
        }

        public void otherFormClosed(object obj, EventArgs args)
        {
            if ((obj is KiesLeerlingenForm && this.leerlingID <= 0 && !((KiesLeerlingenForm) obj).Visible) ||
                (obj is KiesOpdrachtForm && this.opdrachtID <= 0 && !((KiesOpdrachtForm) obj).Visible))
            {
                this.Dispose();
            }
        }

        public void gekozen(String leerlingID, KiesLeerlingenForm form)
        {
            this.leerlingID = Int32.Parse(leerlingID);
            if (form != null)
            {
                form.Dispose();
            }
            Form f = new KiesOpdrachtForm(this);
            f.VisibleChanged += new EventHandler(otherFormClosed);
            f.ShowDialog();
        }

        public void gekozenOpdracht(String opdrachtID, KiesOpdrachtForm form)
        {
            this.opdrachtID = Int32.Parse(opdrachtID);
            form.Dispose();

            opdrachten = acties.getOefeningen(opdrachtID);

            startNextExcersise();
        }

        public void startNextExcersise()
        {
            excersiseNr++;
            timer.Stop();
            if(excersiseNr == opdrachten.Count)
            {
                checkOpdracht(excersiseNr - 1);
                clearScreen(true);
                int height = 300;
                if (opdrachten.Count >= 5)
                {
                    height = 20 + (5 * 105) + 140;
                }
                else if (opdrachten.Count > 2)
                {
                    height = 20 + (opdrachten.Count * 105) + 140;
                }
                this.SetBounds(20, 20, this.Width, height);
                new VScrollProperties(this);
                setOverview();
            }
            else
            {
                this.SetBounds(this.Left, this.Top, this.Width, 300);
                if (excersiseNr > 0)
                {
                    checkOpdracht(excersiseNr - 1);
                    clearScreen(false);
                }
                else
                {
                    answers = new List<String>();
                }

                //TODO - Kaarten tegoei ophalen.
                nextExcersis(opdrachten[excersiseNr].Kaartenlijst);
                cycle = 0;
                pb_Timer.Value = 0;
                timer.Start();
            }
        }

        private void setOverview()
        {
            //TODO
            int row = 0;
            foreach(var opdracht in opdrachten){
                int column = 0;
                //TODO
                List<Kaart> kaarten = new List<Kaart>();
                foreach (Kaart kaart in opdracht.Kaartenlijst)
                {
                    String url = MainKaart.ZoekKaartBeeld(kaart);
                    kaarten.Add(kaart);
                    PictureBox box = new PictureBox();
                    box.Image = new Bitmap(url);
                    box.Name = "PictureBoxEigen" + column;
                    box.SetBounds(20 + (column * 85), 20 + (row * 105), 75, 95);
                    this.Controls.Add(box);
                    
                    column++;
                }
                Label explanation = new Label();
                explanation.Text = answers[row] + "";
                explanation.TextAlign = ContentAlignment.MiddleCenter;
                explanation.SetBounds(20 + (column * 85), 20 + (row * 105), 75, 95);

                List<int> punten = opdracht.Antwoorden;

                int som;

                if (Int32.TryParse(answers[row], out som) && punten.IndexOf(som) > -1)
                {
                    explanation.ForeColor = Color.Green;
                }
                else
                {
                    explanation.ForeColor = Color.Red;
                }
                
                this.Controls.Add(explanation);

                row++;
            }
            Button btn_Again = new Button();
            btn_Again.Text = "Nog een oefening";
            btn_Again.Click += btn_Again_Click;
            btn_Again.SetBounds(this.Width - 180, this.Height - 150, 150, 100);
            this.Controls.Add(btn_Again);
            Button btn_Close = new Button();
            btn_Close.Text = "Stop";
            btn_Close.Click += btn_Close_Click;
            btn_Close.SetBounds(this.Width - 200 - btn_Again.Width, this.Height - 150, 150, 100);
            this.Controls.Add(btn_Close);
        }

        public void btn_Close_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        public void btn_Again_Click(object sender, EventArgs e)
        {
            opdrachtID = 0;
            gekozen(leerlingID + "", null);
        }

        private void showBegin()
        {
            for (int i = this.Controls.Count - 1; i >= 0; i--)
            {
                Control c = this.Controls[i];
                if ((c.Name.Equals("tb_Antwoord") || c.Name.Equals("btn_Antwoord") || c.Name.Equals("pb_Timer") || c.Name.Equals("btn_Stop")))
                {
                    c.Show();
                }
            }
        }

        private void clearScreen(Boolean deleteAll)
        {
            for(int i = this.Controls.Count-1;i >= 0;i--){
                Control c = this.Controls[i];
                if((c.Name.Equals("tb_Antwoord") || c.Name.Equals("btn_Antwoord") || c.Name.Equals("pb_Timer") || c.Name.Equals("btn_Stop"))){
                    if ((deleteAll))
                    {
                        c.Hide();
                    }
                }
                else
                {
                    this.Controls.Remove(c);
                }
            }
        }

        private void checkOpdracht(int opdrachtNr)
        {
            String text = tb_Antwoord.Text.Trim();
            tb_Antwoord.Text = "";
            answers.Add(text);

            //Wegschrijven
            acties.vulRekenSomIn(opdrachten[opdrachtNr], leerlingID, text);
        }

        public void nextExcersis(List<Kaart> kaarten)
        {
            for (int i = 0; i < kaarten.Count; i++)
            {
                String url = MainKaart.ZoekKaartBeeld(kaarten[i]);
                PictureBox box = new PictureBox();
                box.Image = new Bitmap(url);
                box.Name = "PictureBoxEigen" + i;
                box.SetBounds(20 + (i * 85), 20, 75, 95);
                this.Controls.Add(box);
                
                Label explanation = new Label();
                explanation.TextAlign = ContentAlignment.MiddleCenter;
                List<int> punten = MainKaart.getKaartPunten(kaarten[i]);
                foreach(int punt in punten){
                    explanation.Text += punt;
                    if (punten.IndexOf(punt) < (punten.Count - 1))
                    {
                        explanation.Text += "\n";
                    }
                }
                explanation.SetBounds(20 + (i * 85), 125, 75, (20 * punten.Count));
                this.Controls.Add(explanation);
            }
        }

        public Int32 getLeerlingID()
        {
            return leerlingID;
        }

        private void btn_Antwoord_Click(object sender, EventArgs e)
        {
            startNextExcersise();
        }

        public void timing (Object o, EventArgs args){
            if (cycle == this.tijd)
            {
                startNextExcersise();
            }
            else
            {
                cycle++;
            }
            pb_Timer.Value = cycle * ((int)Math.Floor((100.0 / (this.tijd))));
        }
    }
}
